package httpService

import (
	"github.com/dchest/captcha"
	"github.com/google/uuid"
	"github.com/labstack/echo/v4"
	"pm/config"
	"pm/db"
	"pm/model"
	"time"
)

const (
	DefaultUserName = "admin"
	DefaultPassword = "123456"
)

func UserLogin(c echo.Context)error{
	username := c.FormValue("username")
	password := c.FormValue("password")
	captchaCode := c.FormValue("captcha_code")
	captchaId := c.FormValue("captcha_id")
	if username == ""|| password == ""{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"用户名或密码不能为空",
		})
	}
	if captchaCode == "" || captchaId == ""{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"验证码校验失败",
		})
	}
	if !captcha.VerifyString(captchaId, captchaCode){
		return c.JSON(200, map[string]interface{}{
			"status": 2,
			"msg":"验证码校验失败",
		})
	}


	var userInfo config.User
	for _,u := range config.ConfigData.Users{
			if u.Name == username && password == u.Passwd{
				userInfo = u
			}
	}
	if userInfo.Name == ""{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"用户不存在或密码错误",
		})
	}
	//var userInfo model.User
	//err := db.NewDb().Model(&model.User{}).Where("username = ?", username).Take(&userInfo).Error
	//if err != nil && err == gorm.ErrRecordNotFound{
	//	if username == DefaultUserName && password == DefaultPassword{
	//		 uid,_ := uuid.NewUUID()
	//		 token := uid.String()
	//		 var userTokenData model.UserToken
	//		userTokenData.Token = token
	//		userTokenData.Name = username
	//		userTokenData.EffectiveTime = int(time.Now().Add(2 * time.Hour).Unix())
	//		err := db.NewDb().Model(&model.UserToken{}).Create(&userTokenData).Error
	//		if err != nil{
	//			return c.JSON(200, map[string]interface{}{
	//				"status": 3,
	//				"msg":"系统错误登录失败2",
	//			})
	//		}
	//		return c.JSON(200, map[string]interface{}{
	//			"status": 0,
	//			"token":token,
	//		})
	//	}
	//}
	//if err != nil{
	//	fmt.Println(err.Error())
	//	return c.JSON(200, map[string]interface{}{
	//		"status": 4,
	//		"msg":"系统错误登录失败1",
	//	})
	//}
	//if userInfo.FailLastTime > time.Now().Unix(){
	//	return c.JSON(200, map[string]interface{}{
	//		"status": 5,
	//		"msg":"错误次数过多请稍后登录",
	//	})
	//}
	//if userInfo.Password != password{
	//	userInfo.FailTimes++
	//	if userInfo.FailTimes > 10{
	//		userInfo.FailLastTime = time.Now().Add(12 *time.Hour).Unix()
	//	}
	//	if userInfo.FailTimes > 6{
	//		userInfo.FailLastTime = time.Now().Add(2 *time.Hour).Unix()
	//	}
	//	if userInfo.FailTimes > 5{
	//		userInfo.FailLastTime = time.Now().Add(10 *time.Minute).Unix()
	//	}
	//	db.NewDb().Model(&model.User{}).Save(userInfo)
	//	return c.JSON(200, map[string]interface{}{
	//		"status": 6,
	//		"msg":"密码错误",
	//	})
	//}
	//userInfo.FailTimes = 0
	//userInfo.FailLastTime = 0
	//db.NewDb().Model(&model.User{}).Save(userInfo)
	uid,_ := uuid.NewUUID()
	token := uid.String()
	var userTokenData model.UserToken
	db.NewDb().Model(&model.UserToken{}).Where("name = ?", username).Take(&userTokenData)
	userTokenData.Token = token
	userTokenData.Name = username
	userTokenData.EffectiveTime = int(time.Now().Add(12 * time.Hour).Unix())
	err := db.NewDb().Save(&userTokenData).Error
	if err != nil{
		return c.JSON(200, map[string]interface{}{
			"status": 3,
			"msg":"系统错误登录失败2",
		})
	}
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"token":token,
	})

}


func LogOut(c echo.Context)error{
	token := c.Request().Header.Get("token")
	var userTokenData model.UserToken
	err := db.NewDb().Model(&model.UserToken{}).Where("token = ?", token).Delete(&userTokenData).Error
	if err != nil{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":err.Error(),
		})
	}
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"msg":"",
	})
}
